package com.xinzhi.utils;


import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.symmetric.AES;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.*;
import java.io.*;
import java.security.NoSuchAlgorithmException;


public class AESFileUtil {


    private static final Logger LOGGER = LoggerFactory.getLogger(AESFileUtil.class);
    private static AES initAES(String key) {
        AES aes =  new AES(Mode.CBC, Padding.ISO10126Padding, key.getBytes(),key.getBytes());
        return aes;
    }


    /**
     * AES 加密
     * @param encryptPath
     * @param decryptPath
     * @param sKey
     * @return
     */
    public static boolean encryptFile(String encryptPath, String decryptPath, String sKey) throws NoSuchPaddingException, NoSuchAlgorithmException {
        File encryptFile = null;
        File decryptfile = null;
        BufferedOutputStream bufferedOutputStream = null;
        BufferedInputStream bufferedInputStream = null;
        try {
            encryptFile = new File(encryptPath);
            if(!encryptFile.exists()) {
                throw  new NullPointerException("Encrypt file is empty");
            }
            decryptfile = new File(decryptPath);
            if(decryptfile.exists()) {
                decryptfile.delete();
            }
            decryptfile.createNewFile();
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(decryptfile));
            bufferedInputStream = new BufferedInputStream(new FileInputStream(encryptFile));
            AES aes = initAES(sKey);
            aes.encrypt(bufferedInputStream);
        } catch (IOException e) {
            FileUtil.del(decryptfile.getAbsolutePath());
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return false;
        }
        return true;
    }

    /**
     * AES 解密
     * @param encryptFile
     * @param decryptFile
     * @param mKey
     * @return
     */
    public static String decryptFile(File encryptFile,  File decryptFile, String mKey) throws NoSuchPaddingException, NoSuchAlgorithmException {
        LOGGER.info("解密密钥>>>>>>>>>>>>>>>>>>>>> {}",mKey);
        BufferedOutputStream outputStream = null;
        BufferedInputStream inputStream = null;
        //CipherInputStream inputStream = null;
        String md5Hex = null;
        try {
            if (!encryptFile.exists()) {
                throw new NullPointerException("Decrypt file is empty");
            }
            if (decryptFile.exists()) {
                decryptFile.delete();
            }
            decryptFile.createNewFile();
            AES aes = initAES(mKey);
            outputStream = new BufferedOutputStream(new FileOutputStream(decryptFile));
            inputStream = new BufferedInputStream(new FileInputStream(encryptFile));
            aes.decrypt(inputStream);
            //解密后文件MD5 值，用于判断与原文件是否一致
            md5Hex = DigestUtil.md5Hex(decryptFile);
        } catch (Exception e) {
            LOGGER.info("解密失败>>>>>>>>{}",e.getMessage());
        } finally {
            LOGGER.info("删除原加密文件>>>>>>>{}",encryptFile.getAbsolutePath());
        }
        return md5Hex;
    }

}
